#include "afxinet.h"
#include "HttpRequest.h"
#include "HttpUtil.h"

int HttpRequest::fetchUrl()
{
	m_vecText.clear();

	char* headers="Accept:*/*\r\nAccept-Language:zh-cn\r\nUser-Agent:HouseRobot\r\n";
	CInternetSession internetSession;
	CHttpFile* pHttpFile = (CHttpFile*)internetSession.OpenURL(m_strUrl.c_str(), 1, INTERNET_FLAG_TRANSFER_ASCII || INTERNET_FLAG_RELOAD, headers, strlen(headers));
	DWORD dwStatusCode;
	pHttpFile->QueryInfoStatusCode(dwStatusCode);
	if (dwStatusCode != HTTP_STATUS_OK)
	{
		return 1;
	}

	CString szData;
	while(pHttpFile->ReadString(szData))
	{
		m_vecText.push_back(szData.GetBuffer(0));
	}
	pHttpFile->Close();
	internetSession.Close();

    return 0;
}

int HttpRequest::getLineCount()
{
    return (int)m_vecText.size();
}

std::vector<std::string> HttpRequest::getText()
{
    return m_vecText;
}

std::string HttpRequest::getline(unsigned int uiLineNum)
{
    if ((uiLineNum - 1) >= m_vecText.size())
    {
        return "";
    }

    return m_vecText.at(uiLineNum - 1);
}
